package com.betterjr.common.data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ConfigurableApplicationContext;

import com.betterjr.modules.cert.entity.CustCertRule;

public enum PlatformBaseRuleType {
    NONE_USER("陌生人"), CORE_USER("核心企业"), SUPPLIER_USER("供应商"), FACTOR_USER("资金方"), SELLER_USER("经销商"), PLATFORM_USER(
            "平台"), WOS("电子合同服务商");

    private final String title;

    PlatformBaseRuleType(final String anTitle) {
        this.title = anTitle;
    }

    public String getTitle() {
        return this.title;
    }

    public static PlatformBaseRuleType checking(final String anWorkType) {
        try {
            if (StringUtils.isNotBlank(anWorkType)) {

                return PlatformBaseRuleType.valueOf(anWorkType.trim().toUpperCase());
            }
        }
        catch (final Exception ex) {

        }
        return NONE_USER;
    }

    public static List<PlatformBaseRuleType> checkList(final List<CustCertRule> anCertRules) {
        final List<PlatformBaseRuleType> result = new ArrayList<>();

        for (final CustCertRule certRule : anCertRules) {
            if (StringUtils.isNotBlank(certRule.getRule())) {
                final PlatformBaseRuleType tmpRule = checking(certRule.getRule());
                if (tmpRule != NONE_USER) {
                    result.add(tmpRule);
                }
            }
        }

        return result;
    }

    public static boolean checkValue(String value) {
        PlatformBaseRuleType[] values = PlatformBaseRuleType.values();
        for (PlatformBaseRuleType platformBaseRuleType : values) {
            if (platformBaseRuleType.name().equals(value)) return true;
        }

        return false;
    }

    public static String valuesToString(List<PlatformBaseRuleType> types) {

        StringBuilder sb = new StringBuilder();

        for (PlatformBaseRuleType platformBaseRuleType : types) {
            sb.append(platformBaseRuleType.name()).append(",");
        }

        if (sb.length() > 0) {
            return sb.substring(0, sb.length() - 1);
        }

        return sb.toString();
    }

    public static List<PlatformBaseRuleType> ruleStringToList(String anRuleList) {
        List<String> list = Arrays.asList(org.springframework.util.StringUtils.tokenizeToStringArray(anRuleList,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
        List<PlatformBaseRuleType> platformList = new ArrayList<PlatformBaseRuleType>();
        for (String str : list) {
            if (PlatformBaseRuleType.checkValue(str)) {

                platformList.add(PlatformBaseRuleType.valueOf(str.trim().toUpperCase()));
            }
        }
        return platformList;
    }

    public static void main(String[] args) {
        System.out.println(checkValue("CORE_USER"));

        List<PlatformBaseRuleType> asList = Arrays.asList(PlatformBaseRuleType.values());
        String valuesToString = valuesToString(asList);
        System.out.println(valuesToString);
        System.out.println("-----------");
        List<PlatformBaseRuleType> ruleStringToList = ruleStringToList(
                "NONE_USER,CORE_USER,SUPPLIER_USER,FACTOR_USER,SELLER_USER,PLATFORM_USER,WOS");
        for (PlatformBaseRuleType platformBaseRuleType : ruleStringToList) {
            System.out.println(platformBaseRuleType.name());
        }
    }

}
